package admin

import (
	"fmt"
	"news_for_deepseek_interface/config"
	"news_for_deepseek_interface/controller"

	"github.com/gin-gonic/gin"

	billing "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/billing/v20180709"
	"github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common"
	"github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/errors"
	"github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/profile"
)

func Balance(c *gin.Context) {

	credential := common.NewCredential(
		config.DefaultConfig.Tencent.SecretId,
		config.DefaultConfig.Tencent.SecretKey,
	)
	// 实例化一个client选项，可选的，没有特殊需求可以跳过
	cpf := profile.NewClientProfile()
	cpf.HttpProfile.Endpoint = "billing.tencentcloudapi.com"
	// 实例化要请求产品的client对象,clientProfile是可选的
	client, _ := billing.NewClient(credential, "ap-guangzhou", cpf)
	request := billing.NewDescribeAccountBalanceRequest()

	// 返回的resp是一个DescribeAccountBalanceResponse的实例，与请求对象对应
	response, err := client.DescribeAccountBalance(request)
	if _, ok := err.(*errors.TencentCloudSDKError); ok {
		controller.Fail(c, controller.BusinessError, err.Error())
		return
	}
	if err != nil {
		controller.Fail(c, controller.BusinessError, err.Error())
		return
	}

	//保留2位小数
	balance := fmt.Sprintf("%.2f", float64(*response.Response.Balance)/100)

	controller.Success(c, balance)
}
